package com.concurrent.chapter8.concurrent01;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Main {
	public static void main(String[] args) {
		try {
			List<Callable<String>> callables = new ArrayList<>();
			callables.add(new MyCallableA());
			callables.add(new MyCallableB());
			int corePoolSize = 2;
			//ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(corePoolSize);
			ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
			long delay = 4;//延迟4秒开始执行
			TimeUnit unit = TimeUnit.SECONDS;
			ScheduledFuture<String> scheduledFutureA = scheduledExecutorService.schedule(callables.get(0), delay, unit);
			ScheduledFuture<String> scheduledFutureB = scheduledExecutorService.schedule(callables.get(1), delay, unit);
			System.out.println("x=" + System.currentTimeMillis());//x=1473036674111
			System.out.println("A:" + scheduledFutureA.get());
			System.out.println("B:" + scheduledFutureB.get());
			System.out.println("y=" + System.currentTimeMillis());//1473036681107
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
